package com.vitabuybuy.tv.network

import com.library.common.net.NetUrl
import com.vitabuybuy.tv.dto.AddressListDto
import com.vitabuybuy.tv.dto.RegionDto
import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 作者：SimGa
 * 日期：2020/12/3
 * 时间：17:04
 * 包名：com.vitabuybuy.tv.network
 */
object AddressRepository {

    fun getAddressList(): IAwait<MutableList<AddressListDto>> {
        return RxHttp.postJson(NetUrl.api_get_address_list).toResponse()
    }

    fun getRegionList(regionId: Int?): IAwait<MutableList<RegionDto>> {
        return RxHttp.postJson(NetUrl.api_get_region)
            .add("region", if (regionId == null) null else mapOf(Pair("id", regionId)))
            .toResponse()
    }

    fun saveOrupdateAddress(
        receiverName: String,
        receiverPhone: String,
        regionId: Int,
        detailArea: String,
        zipCode: String,
        isDefault: Boolean?,
        id: Int
    ): IAwait<String> {
        return RxHttp.postJson(if (id == -1) NetUrl.api_save_address else NetUrl.api_update_address)
            .add(
                "address", mapOf(
                    Pair("name", receiverName),
                    Pair("phone", receiverPhone),
                    Pair("detail", detailArea),
                    Pair("zipcode", zipCode),
                    Pair("defaulted", isDefault),
                    Pair("region", mapOf(Pair("id", regionId))),
                    Pair("id", id)
                )
            )
            .toResponse()
    }

    fun deleteAddress(id: Int?): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_delete_address)
            .add("id", id)
            .toResponse()
    }
}